615C - Running Track - CodeForces Solution


dp greedy strings trees *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
// AC2#
using namespace std;

using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;

#define fi first
#define se second
#define pb push_back
#define SZ(x) int((x).size())
#define ALL(x) begin(x), end(x)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ENDL '\n'

void buildPi(string& p, vi& pi) {
    pi = vi(SZ(p));
    int k = -2;
    FOR(i, 0, SZ(p)) {
        while (k >= -1 && p[k + 1] != p[i])
            k = (k == -1) ? -2 : pi[k];
        pi[i] = ++k;
    }
}

pi kmp(string& t, string& p) {
    if (p == "")
        return {0, 0};

    vi pi;
    buildPi(p, pi);
    int k = -1;
    FOR(i, 0, SZ(t)) {
        while (k >= -1 && p[k + 1] != t[i])
            k = (k == -1) ? -2 : pi[k];
        k++;
        if (k == SZ(p) - 1) {
            return {i - k, i - k + SZ(p) - 1};
        }
    }
    return {-1, -1};
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    string s, t;
    cin >> s >> t;
    string s_i = s;
    reverse(ALL(s_i));

    vector<bool> in_s(SZ(s), 0);
    FOR(i, 0, SZ(s)) {
        in_s[s[i] - 'a'] = 1;
    }

    FOR(i, 0, SZ(t)) {
        if (!in_s[t[i] - 'a']) {
            cout << -1 << ENDL;
            return 0;
        }
    }

    t += '#';

    int ind = 0;
    vector<pi> coatings;
    while (ind < SZ(t) - 1) {
        string aux = "";
        int last_l, last_r;
        while (1) {
            auto [l1, r1] = kmp(s, aux);
            auto [l2, r2] = kmp(s_i, aux);

            if (l1 != -1) {
                last_l = l1;
                last_r = r1;
            } else if (l2 != -1) {
                last_l = SZ(s) - l2 - 1;
                last_r = SZ(s) - r2 - 1;
            }

            if (l1 == -1 && l2 == -1) {
                ind--;
                coatings.pb({last_l + 1, last_r + 1});
                break;
            }
            aux += t[ind++];
        }
    }

    cout << SZ(coatings) << ENDL;
    for (auto& [u, v] : coatings)
        cout << u << ' ' << v << ENDL;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent